﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using YT.DB;
using YT.Modules;
using YT.Commons; 
 
namespace YT.XH.SuddenDeafness
{
    public partial class frmContraindicationMtn : Form
    {

        private string mstrSickRecID = string.Empty;
        private bool mblnFill = false;
        public frmContraindicationMtn()
        {
            InitializeComponent();
            mstrSickRecID = SysVar.SickRecID;
        }

        public frmContraindicationMtn( string strSickRecID )
        {
            InitializeComponent();
            mstrSickRecID = strSickRecID;
        }

        public void FillContraindication(string strSickRecID)
        {
            mstrSickRecID = strSickRecID;
            mblnFill = true;
            FillData();
            mblnFill = false;
        }

        public void SetReadonly( bool blnTrue)
        {
            chkA.Enabled = blnTrue;
            chkB.Enabled = blnTrue;
            chkC.Enabled = blnTrue;
            chkD.Enabled = blnTrue;

            groupA.Enabled = blnTrue;
            groupB.Enabled = blnTrue;
            groupC.Enabled = blnTrue;
            groupD.Enabled = blnTrue;
        }

        private void initControl()
        {
            chkA.Checked = false;            
            chkB.Checked = false;
            chkC.Checked = false;
            chkD.Checked = false;

            foreach (Control con in groupA.Controls)
            {
                if (con.GetType().Name == "CheckBox")
                {
                    ((CheckBox)con).Checked = false;
                }
            }
            foreach (Control con in groupB.Controls)
            {
                if (con.GetType().Name == "CheckBox")
                {
                    ((CheckBox)con).Checked = false;
                }
            }
            foreach (Control con in groupC.Controls)
            {
                if (con.GetType().Name == "CheckBox")
                {
                    ((CheckBox)con).Checked = false;
                }
            }
            foreach (Control con in groupD.Controls)
            {
                if (con.GetType().Name == "CheckBox")
                {
                    ((CheckBox)con).Checked = false;
                }
            }

            this.tabControl1.TabPages.Remove(tabPage3); 
        }

        private bool FillData()
        {
            initControl();
            DBAccess dbcon =new DBAccess (false,false );
            string strSQL = "select * from mContraindication where sickRecID='" + mstrSickRecID  + "'";
            DataTable dtTable = dbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtTable.Rows)
            {
                if (CvtFunc.ToString(drRow["ContraindicationType"]) == "A")
                {
                    chkA.Checked = true;
                    FillDtl(dbcon , this.groupA);
                }
                if (CvtFunc.ToString(drRow["ContraindicationType"]) == "B")
                {
                    chkB.Checked = true;
                    FillDtl(dbcon, this.groupB );
                }
                if (CvtFunc.ToString(drRow["ContraindicationType"]) == "C")
                {
                    chkC.Checked = true;
                    FillDtl(dbcon, this.groupC);
                }
                if (CvtFunc.ToString(drRow["ContraindicationType"]) == "D")
                {
                    chkD.Checked = true;
                    FillDtl(dbcon, this.groupD);
                }
            }
            return true;
        }

        private void FillDtl(DBAccess dbcon,  Control  ConName)
        {
            string strSQL = string.Empty;
            if (ConName.Name == this.groupA.Name)
                strSQL = "select * from mContraindicationDetail where HdrContraindication='A' and SickRecID='" + mstrSickRecID + "'";
            else if (ConName.Name == this.groupB.Name)
                strSQL = "select * from mContraindicationDetail where HdrContraindication='B' and SickRecID='" + mstrSickRecID + "'";
            else if (ConName.Name == this.groupC.Name)
                strSQL = "select * from mContraindicationDetail where HdrContraindication='C' and SickRecID='" + mstrSickRecID + "'";
            else if (ConName.Name == this.groupD.Name)
                strSQL = "select * from mContraindicationDetail where HdrContraindication='D' and SickRecID='" + mstrSickRecID + "'";
            else
                return;
 
            DataTable dtDtl = dbcon.GetDataTable(strSQL);
            foreach (DataRow drRow in dtDtl.Rows)
            {
                foreach (Control con in ConName.Controls)
                {
                    string strDtlContraindicationCode = CvtFunc.ToString(drRow["DtlContraindication"]);
                    string strDtlContraindicationName = CvtFunc.ToString(drRow["DtlContraindicationName"]);
                    if (con.GetType().Name == "CheckBox" && strDtlContraindicationCode == con.Name.Substring(3, con.Name.Length - 3))
                    {
                        ((CheckBox)con).Checked = true;
                        if (con.Name == chkA10.Name)
                            txtA10.Text = strDtlContraindicationName;
                        else if (con.Name == chkB9.Name)
                            txtB9.Text = strDtlContraindicationName;
                        else if (con.Name == chkD8.Name)
                            txtD8.Text = strDtlContraindicationName;
                    }
                }
            }
        }

        private void UpdateDtl(DBAccess dbcon, Control con,string strHdrContraindicationCode,string strHdrContraindicationName)
        {
            string strDtlFields = "SickRecID,HdrContraindication,HdrContraindicationName,DtlContraindication,DtlContraindicationName,Type";
            if (con.GetType().Name == "CheckBox")
            {
                if (((CheckBox)con).Checked)
                {
                    string strSQL = "select top 0 * from mContraindicationDetail";
                    DataRow drDtlRow = dbcon.GetDataTable(strSQL).NewRow();
                    string strDtlContraindicationCode = con.Name.Substring(3, con.Name.Length - 3);
                    string strDtlContraindicationName = string.Empty;
                    string strDtlType = TitleType.SuddenDeafness.ToString();
                    if (con.Name == chkA10.Name)
                        strDtlContraindicationName = txtA10.Text;
                    else if (con.Name == chkB9.Name)
                        strDtlContraindicationName = txtB9.Text;
                    else if (con.Name == chkD8.Name)
                        strDtlContraindicationName = txtD8.Text;
                    else
                        strDtlContraindicationName = con.Text;
                    drDtlRow["SickRecID"] = mstrSickRecID;
                    drDtlRow["HdrContraindication"] = strHdrContraindicationCode;
                    drDtlRow["HdrContraindicationName"] = strHdrContraindicationName;
                    drDtlRow["DtlContraindication"] = strDtlContraindicationCode;
                     
                    drDtlRow["DtlContraindicationName"] = strDtlContraindicationName; ;
                    drDtlRow["Type"] = TitleType.SuddenDeafness.ToString();
                    DataFunctions.UpdateTable(dbcon, strDtlFields, "mContraindicationDetail", drDtlRow, FormStatus.AddNew);
                }
            }
        }
        public  void  UpdateData()
        {
            if (mblnFill) return;
            bool blnIsNew = false;
            string strSQL = string.Empty;            
            string strFields = "SickRecID,Type,ContraindicationType" ;
            FormStatus mAddStatus = new FormStatus();

            DBAccess dbcon=new DBAccess (false,false );
            strSQL = "Delete from mContraindication where sickRecID='" + mstrSickRecID + "'";
            dbcon.ExecuteSql(strSQL);
            strSQL = "delete from mContraindicationDetail where SickRecID='" + mstrSickRecID + "'";
            dbcon.ExecuteSql(strSQL);

            strSQL = "select * from mContraindication where sickRecID='" + mstrSickRecID + "'";
            DataRow drRow = dbcon.GetDataFirstRow(strSQL);
            if (drRow != null)
                mAddStatus = FormStatus.Update;
            else
                mAddStatus = FormStatus.AddNew;

            if (drRow == null) drRow = dbcon.GetDataTable(strSQL).NewRow();  
            string strHosptialID = SysVar.HospitalID;
            string strSickRecID = mstrSickRecID;
            string strType =CvtFunc.ToString ( TitleType.SuddenDeafness )  ;  //在这里是200,表示是突聋。
            string mContraindicationType = string.Empty;
                        
            drRow["SickRecID"] = strSickRecID;
            drRow["Type"] = strType;            
            if (chkA.Checked == true)
            {
                mContraindicationType = "A";
                drRow["ContraindicationType"] = mContraindicationType;
                DataFunctions.UpdateTable(dbcon, strFields, "mContraindication", drRow, mAddStatus);
                #region udpate mContraindicationDetail               
                {
                    foreach (Control con in groupA.Controls)
                    {
                        string strHdrContraindicationCode = "A";
                        string strHdrContraindicationName = "口服激素";                         
                        UpdateDtl(dbcon, con, strHdrContraindicationCode, strHdrContraindicationName); 
                    }
                }
                #endregion
            }

            if (chkB.Checked == true)
            {
                mContraindicationType = "B";
                drRow["ContraindicationType"] = mContraindicationType;
                DataFunctions.UpdateTable(dbcon, strFields, "mContraindication", drRow, mAddStatus);
                #region udpate mContraindicationDetail
                {
                    foreach (Control con in groupB.Controls)
                    {
                        if (con.GetType().Name == "CheckBox")
                        {
                            string strHdrContraindicationCode = "B";
                            string strHdrContraindicationName = "经鼓室给激素";
                            UpdateDtl(dbcon, con, strHdrContraindicationCode, strHdrContraindicationName); 
                        }
                    }
                }
                #endregion
            }

            if (chkC.Checked == true)
            {
                mContraindicationType = "C";
                drRow["ContraindicationType"] = mContraindicationType;
                DataFunctions.UpdateTable(dbcon, strFields, "mContraindication", drRow, mAddStatus);
                #region udpate mContraindicationDetail
                {
                    foreach (Control con in groupC.Controls)
                    {
                        if (con.GetType().Name == "CheckBox")
                        {
                            string strHdrContraindicationCode = "C";
                            string strHdrContraindicationName = "高压氧";
                            UpdateDtl(dbcon, con, strHdrContraindicationCode, strHdrContraindicationName); 
                        }
                    }
                }
                #endregion
            }

            if (chkD.Checked == true)
            {
                mContraindicationType = "D";
                drRow["ContraindicationType"] = mContraindicationType;
                DataFunctions.UpdateTable(dbcon, strFields, "mContraindication", drRow, mAddStatus);
                #region udpate mContraindicationDetail
                {
                    foreach (Control con in groupD.Controls)
                    {
                        if (con.GetType().Name == "CheckBox")
                        {
                            string strHdrContraindicationCode = "D";
                            string strHdrContraindicationName = "降纤药物";
                            UpdateDtl(dbcon, con, strHdrContraindicationCode, strHdrContraindicationName); ;
                        }
                    }
                }
                #endregion
            }
            //return true;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            //if (!UpdateData()) return;           
            UpdateData();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close(); 
        }

        private void FrmContraindicationMtn_Load(object sender, EventArgs e)
        {            
            FillData();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void frmContraindicationMtn_FormClosed(object sender, FormClosedEventArgs e)
        {
            SysVar.marrFormList.Remove(this.Name);
        }

        private void chkA1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox radNew = sender as CheckBox;
            if (radNew.Checked) chkA.Checked = true;
            UpdateData();
        }

        private void chkB1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox radNew = sender as CheckBox;
            if (radNew.Checked) chkB.Checked = true;
            UpdateData();
        }

        private void chkC1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox radNew = sender as CheckBox;
            if (radNew.Checked) chkC.Checked = true;
            UpdateData();
        }

        private void chkD1_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox radNew = sender as CheckBox;
            if (radNew.Checked) chkD.Checked = true;
            UpdateData();
        }

       
    }
}
